//package com.choosefine.base.shixiaobao.listener;
//
//import java.io.IOException;
//import java.net.Inet4Address;
//import java.net.Inet6Address;
//import java.net.InetAddress;
//import java.net.NetworkInterface;
//import java.net.SocketException;
//import java.net.URL;
//import java.util.Enumeration;
//import java.util.LinkedList;
//import java.util.List;
//import java.util.ResourceBundle;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import com.choosefine.base.shixiaobao.common.def.CommonConstant;
//
//import Com.FirstSolver.Splash.FaceId_Item;
//import Com.FirstSolver.Splash.ISocketServerThreadTask;
//import Com.FirstSolver.Splash.NetworkStreamPlus;
//import Com.FirstSolver.Splash.TcpListenerPlus;
//
///**
// * Created by guwenwei on 2017/5/13 0013.
// */
//public class AttendanceListener implements Initializable,ISocketServerThreadTask{
//
//    Logger logger= LoggerFactory.getLogger(AttendanceListener.class);
//
//    public TcpListenerPlus tcpServer = null;
//
//    private boolean IsPostPhoto = true;
//
//    public boolean isServerRunning() {
//        return IsServerRunning;
//    }
//
//    private boolean IsServerRunning = false;
//
//    public AttendanceListener(String serverhost,String serverport) throws IOException ,Exception{
//        //创建侦听服务器
//        tcpServer = new TcpListenerPlus(Integer.parseInt(serverport),0, InetAddress.getByName(serverhost),true);
//        //设置通信线程任务委托
//        tcpServer.ThreadTaskDelegate = this;
//        //开启侦听服务
//        tcpServer.StartListenThread(null,0,0);
//        IsServerRunning = true;
//    }
//
//    public boolean isPostPhoto() {
//        return IsPostPhoto;
//    }
//
//    public void setPostPhoto(boolean postPhoto) {
//        IsPostPhoto = postPhoto;
//    }
//
//    @Override
//    public void OnServerTaskRequest(NetworkStreamPlus stream) throws Exception {
//        // 设备特殊通信密码
//        // stream.setSecretKey(textFieldSecretKey.getText());  // 注意：密码要和设备通信密码一致
//
//        String SerialNumber;    // 设备序列号
//        while(true)
//        {
//            try {
//                // 读取数据
//                String answer = stream.Read(CommonConstant.CHASET_GBK);
//
//                // 显示读取信息
//                logger.info(answer);
//
//                if(answer.startsWith("PostRecord"))
//                {   // 提取序列号并保存
//                    SerialNumber = FaceId_Item.GetKeyValue(answer, "sn");
//
//                    // 答复已准备好接收考勤记录
//                    if (IsPostPhoto)
//                    {
//                        stream.Write("Return(result=\"success\" postphoto=\"true\")", CommonConstant.CHASET_GBK);
//                    }
//                    else
//                    {
//                        stream.Write("Return(result=\"success\" postphoto=\"false\")", CommonConstant.CHASET_GBK);
//                    }
//                }
//                else if(answer.startsWith("Record"))
//                {   // 读取考勤记录
//
//                    // 服务器回应
//                    stream.Write("Return(result=\"success\")", CommonConstant.CHASET_GBK);
//                }
//                else if(answer.startsWith("PostEmployee"))
//                {   // 准备上传人员信息
//
//                    // 服务器回应
//                    stream.Write("Return(result=\"success\")", CommonConstant.CHASET_GBK);
//                }
//                else if(answer.startsWith("Employee"))
//                {   // 读取人员信息
//
//                    // 服务器回应
//                    stream.Write("Return(result=\"success\")", CommonConstant.CHASET_GBK);
//                }
////                else if (answer.startsWith("GetRequest"))
////                {   // 下发命令
////                    Platform.runLater(() -> {
////                        String Command = textAreaCommand.getText();
////                        if (!Utils.IsNullOrEmpty(Command))
////                        {
////                            try
////                            {
////                                stream.Write(Command, CommonConstant.CHASET_GBK);
////                            }
////                            catch (Exception ex)
////                            {
////
////                            }
////
////                            textAreaCommand.clear();
////                        }
////                    });
////                }
//                else if(answer.startsWith("Quit"))
//                {   // 结束会话
//                    break;
//                }
//            }
//            catch (Exception ex)
//            {
//                break;  // 连接断开
//            }
//        }
//    }
//
//    @Override
//    public void initialize(URL url, ResourceBundle rb) {
//        // 设置服务器地址
//        try
//        {
//            List<String> IPList = new LinkedList<>();
//            Enumeration<NetworkInterface> InterfaceList = NetworkInterface.getNetworkInterfaces();
//            while (InterfaceList.hasMoreElements())
//            {
//                NetworkInterface iFace = InterfaceList.nextElement();
//                if(iFace.isLoopback() || iFace.isVirtual() || iFace.isPointToPoint() || !iFace.isUp()) continue;
//
//                Enumeration<InetAddress> AddrList = iFace.getInetAddresses();
//                while (AddrList.hasMoreElements())
//                {
//                    InetAddress address = AddrList.nextElement();
//                    if ((address instanceof Inet4Address) || (address instanceof Inet6Address))
//                    {
//                        IPList.add(address.getHostAddress());
//                    }
//                }
//            }
//
////            if(!IPList.isEmpty())
////            {
////                comboBoxServerIP.setItems(FXCollections.observableList(IPList));
////                comboBoxServerIP.setValue(IPList.get(0));
////            }
//        }
//        catch (SocketException ex)
//        {
//            // 异常处理
//        }
//    }
//}
